// Decompiled by DJ v3.10.10.93 Copyright 2007 Atanas Neshkov  Date: 27/11/2008 22:52:48
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Game.java

package reversi;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Vector;

// Referenced classes of package reversi:
//            Informer, GameController, Move, ReversiAlgorithm

public class Game extends Thread
    implements GameController
{

    public Game(ReversiAlgorithm areversialgorithm[], int i)
    {
        currentPlayer = 0;
        readyForNext = false;
        listenerList = new Vector();
        currentMove = null;
        lastMove = null;
        turnLength = i * 1000;
        algorithms = areversialgorithm;
    }

    public void doMove(Move move)
    {
        if(move != null && move.player != currentPlayer)
        {
            return;
        } else
        {
            moveDone = true;
            currentMove = move;
            return;
        }
    }

    public Move getLastMove()
    {
        return lastMove;
    }

    public int getCurrentPlayer()
    {
        return currentPlayer;
    }

    void setCurrentPlayer(int i)
    {
        currentPlayer = i;
    }

    void setReadyForNext(boolean flag)
    {
        readyForNext = flag;
    }

    public void addPropertyChangeListener(PropertyChangeListener propertychangelistener)
    {
        listenerList.addElement(propertychangelistener);
    }

    public void removePropertyChangeListener(PropertyChangeListener propertychangelistener)
    {
        listenerList.removeElement(propertychangelistener);
    }

    private void firePropertyChange(String s, Object obj, Object obj1)
    {
        PropertyChangeEvent propertychangeevent = new PropertyChangeEvent(this, s, obj, obj1);
        for(int i = 0; i < listenerList.size(); i++)
            ((PropertyChangeListener)listenerList.elementAt(i)).propertyChange(propertychangeevent);

    }

    void stopIt()
    {
        running = false;
    }

    public void run()
    {
        running = true;
        do
        {
            if(!running)
                break;
            Thread thread = new Thread(algorithms[currentPlayer]);
            thread.start();
            long l = System.currentTimeMillis();
            long l1 = l;
            int i = 20;
            while(l1 - l < (long)turnLength && !moveDone && running) 
            {
                try
                {
                    Thread.sleep(i);
                }
                catch(InterruptedException interruptedexception) { }
                l1 = System.currentTimeMillis();
                firePropertyChange("time", null, new Float((float)Math.min(turnLength, l1 - l) / (float)turnLength));
            }
            if(!moveDone)
            {
                (new Informer(algorithms[currentPlayer], this)).start();
                try
                {
                    Thread.sleep(300L);
                }
                catch(InterruptedException interruptedexception1) { }
            }
            if(thread.isAlive())
                thread.interrupt();
            if(!running)
                break;
            if(!moveDone)
            {
                firePropertyChange("disqualified", null, new Integer(currentPlayer));
                running = false;
                return;
            }
            readyForNext = false;
            currentPlayer ^= 1;
            moveDone = false;
            firePropertyChange("move", null, currentMove);
            lastMove = currentMove;
            currentMove = null;
            while(!readyForNext && running) 
                try
                {
                    Thread.sleep(100L);
                }
                catch(InterruptedException interruptedexception2) { }
            System.gc();
            try
            {
                Thread.sleep(300L);
            }
            catch(InterruptedException interruptedexception3) { }
        } while(true);
        firePropertyChange("stopped", null, null);
    }

    private ReversiAlgorithm algorithms[];
    private int turnLength;
    private int currentPlayer;
    private boolean running;
    private boolean moveDone;
    private boolean readyForNext;
    private Vector listenerList;
    private Move currentMove;
    private Move lastMove;
}